package com.avic.rong.utils;


import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.R;

/**
 * 只维持一个Toast对象,防止多次弹出
 * created by xtj on 2017/12/5 10:28.
 */
public class ToastTools {

    private static Toast toast = null;

    /**
     * @param context 使用时的上下文
     * @param hint    在提示框中需要显示的文本
     * @return 返回一个不会重复显示的toast
     */
    public static Toast showToast(Context context, String hint) {
//        if (toast == null) {
        Toast toastTemp = Toast.makeText(context, hint, Toast.LENGTH_SHORT);

        LinearLayout linearLayout = (LinearLayout) toastTemp.getView();
        TextView messageTextView = (TextView) linearLayout.getChildAt(0);
        messageTextView.setTextColor(context.getResources().getColor(R.color.white));
//            View view = toast.getView();
        linearLayout.setBackgroundResource(R.drawable.shape_toast_shoucang);
//            lp.alpha = 0.4f;
        linearLayout.setAlpha(0.8f);
        toastTemp.setView(linearLayout);
        toastTemp.setGravity(Gravity.CENTER, 0, 0);
//        } else {
        toastTemp.setText(hint);
//        }

        toastTemp.show();
        return toastTemp;
    }

    /**
     * @param context 使用时的上下文
     * @param hint    在提示框中需要显示的文本
     * @return 返回一个不会重复显示的toast
     */
    public static Toast showLongToast(Context context, String hint) {
        if (toast == null) {
            toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(hint);
        }

        toast.show();
        return toast;
    }

    /**
     * @param context 使用时的上下文
     * @param hint    在提示框中需要显示的文本
     * @return 返回一个不会重复显示的toast
     */
    public static Toast showLongToast(Context context, int hint) {
        if (toast == null) {
            toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(hint);
        }

        toast.show();
        return toast;
    }

    /**
     * @param context 使用时的上下文
     * @param resId   在提示框中需要显示的文本id
     * @return 返回一个不会重复显示的toast
     */
    public static Toast showToast(Context context, int resId) {

        if (toast == null) {
            toast = Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(context.getResources().getString(resId));
        }

        toast.show();
        return toast;
    }

    /**
     * 清除 toast
     */
    public static void cancel() {
        if (toast == null) {
            return;
        }
        toast.cancel();
        toast = null;
    }


    private static Handler mHandler = new Handler();
    private static Runnable runnable = new Runnable() {
        @Override
        public void run() {
            toast.cancel();
            toast = null;// toast隐藏后，将其置为null
        }
    };

    public static void showToast(String message) {
        LayoutInflater inflater = (LayoutInflater) Utils.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;// 自定义布局
        if (inflater != null) {
            view = inflater.inflate(R.layout.toast_addcar_success, null);
            TextView text = view.findViewById(R.id.tv_tip);// 显示的提示文字
            if (!StringUtils.isEmpty(message)) {
                text.setText(message);
            }
        }

        mHandler.removeCallbacks(runnable);
        if (toast == null) {// 只有mToast==null时才重新创建，否则只需更改提示文字
            toast = new Toast(Utils.getContext());
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 100);
            toast.setView(view);
        }
        mHandler.postDelayed(runnable, 2000);// 延迟2秒隐藏toast
        toast.show();
    }

    public static void showPublicToast(String message) {
        LayoutInflater inflater = (LayoutInflater) Utils.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;// 自定义布局
        if (inflater != null) {
            float alpha = 0.7f;
            view = inflater.inflate(R.layout.layout_toast, null);
            view.setAlpha(alpha);
            TextView text = view.findViewById(R.id.tv_content);// 显示的提示文字
            if (!StringUtils.isEmpty(message)) {
                text.setText(message);
            }
        }

        mHandler.removeCallbacks(runnable);
        if (toast == null) {// 只有mToast==null时才重新创建，否则只需更改提示文字
            toast = new Toast(Utils.getContext());
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, -200);
            toast.setView(view);
        }
        mHandler.postDelayed(runnable, 2000);// 延迟2秒隐藏toast
        toast.show();
    }


    public static void showToastCircle(String message) {
        LayoutInflater inflater = (LayoutInflater) Utils.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;// 自定义布局
        if (inflater != null) {
            view = inflater.inflate(R.layout.toast_circle_net, null);
            if (!StringUtils.isEmpty(message)) {
                TextView text = view.findViewById(R.id.toast_message);// 显示的提示文字
                text.setVisibility(View.VISIBLE);
                text.setText(message);
            }
        }

        mHandler.removeCallbacks(runnable);
        if (toast == null) {// 只有mToast==null时才重新创建，否则只需更改提示文字
            toast = new Toast(Utils.getContext());
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.setView(view);
        mHandler.postDelayed(runnable, 2000);// 延迟2秒隐藏toast
        toast.show();
    }

}